﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#define D 1

//string类对象的常见的构造
void test()
{
	//创建一个空串
	string s;
	//用c格式创建一个字符串
	string s2("hello xy");
	//构造函数
	string s3(s2);
	//string类有多少个字符
	string s4(10, 'q');
}

//string 类和对象的容量操作
void test1()
{
	/*string s("hello_xiaoy");
	cout<<s.size()<<endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout << s.empty() << endl;
	s.reserve(100);
	cout << s.size() << endl;
	cout << s.capacity() << endl;*/
	/*s.resize(3);
	s.resize(15, 0);
	s.clear();
	cout << s.capacity() << endl;
	cout << s.size() << endl;*/

	//要满了就要增容
	string s("hello_word");
	size_t sz = s.capacity();
#ifndef D

	//满了就整容
	for (int i = 0; i < 100; i++)
	{
		s.push_back(i);
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << sz << endl;
		}
	}

#endif
	//直接开始开够空间
	s.reserve(100);
	for (int i = 0; i < 100; i++)
	{
		s.push_back(i);
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << sz << endl;

		}
	}
}


//string类和对象的访问及遍历 3种遍历方式
void test2()
{
	////operator[]
	//string s("hello_word");
	//int i = 0;
	//for (i = 0; i < s.size(); i++)
	//{
	//	s[i] += 1;
	//	cout << s[i] << " ";
	//}

	////迭代器
	string s("hello_word");
	//string::iterator it = s.begin();
	//{
	//	while(it != s.end());
	//	{
	//		cout << *it << " "<< endl;
	//		it++;
	//	}
	//}

	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;
	//范围for
	for (auto ch : s)
	{
		cout << ch <<" ";
	}

	cout << endl;

	//反向输出
	string::reverse_iterator it = s.rbegin();
	while (it != s.rend())
	{
		cout << *it << " ";
		*it++;
	}

}

//string 类对象的修改操作
void test3()
{
	////后面+一个字符
	//string s("hello_xy");
	//s.push_back('a');
	//s.push_back('b');
	//s.push_back('c');
	//cout << s << endl;
	////后面+一个字符串
	//s.append("xioayb");
	//cout << s << endl;

	////推荐使用这个operator+=
	//s += '2';
	//s += "niaho";
	//cout << s << endl;

	//string s("strttstrbbstr");
	//cout << s.c_str() << endl;
	//
	////npos 是一个无符号整形 最大值4294967295,
	//size_t start = s.find('b',7);
	//cout << start << endl;
	//start = s.rfind('b', 7);
	//cout << start << endl;

	//解析这个网址。 协议 域名 资源名
	string s("https://www.cplusplus.com/reference/string/string/find/");
	size_t start = s.find("://");
	if (start == string::npos)	//没有找到
	{
		cout << "invalid url" << endl;
		return;
	}
	else
	{
		string subl = s.substr(0,start);
		cout << subl << endl;
	}

	size_t finish = s.find('/', start + 3);
	if (finish == string::npos)	//没有找到
	{
		cout << "invalid url" << endl;
		return;
	}
	else
	{
		string sub2 = s.substr(start + 3, finish - (start + 3));
		cout << sub2 << endl;
	}

	string sub3 = s.substr(finish + 1);
	cout << sub3 << " ";
}

void test4()
{


}

int main()
{
	//test();
	//test1();
	test2();
	//test3();
	return 0;
}